/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.ejb.EJB;
import model.*;
import java.util.*;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author shinji
 */
@Named(value = "a_countryManagedBean")
@SessionScoped
public class CountryManagedBean implements Serializable {
    @EJB
    private StateFacade stateFacade;
    @EJB
    private CountryFacade countryFacade;
    
    private Country obj = new Country();
    
    private String Name;
    private String selectCountry;
    private String selectState;
    
    private Map<String, String> mapCountry = new HashMap<String, String>();
    private Map<String, String> mapState = new HashMap<String, String>();
    private Map<String, Map<String, String>> suburbsData = new HashMap<String, Map<String, String>>();
    
    private List<Country> list = new ArrayList<Country>();
    private List<State> listState = new ArrayList<State>();

    public List<State> getListState() {
        return listState;
    }

    public void setListState(List<State> listState) {
        this.listState = listState;
    }
    
    public Country getObj() {
        return obj;
    }

    public void setObj(Country obj) {
        this.obj = obj;
    }

    public List<Country> getList() {
        return list;
    }

    public void setList(List<Country> list) {
        this.list = list;
    }

    public String getSelectCountry() {
        return selectCountry;
    }

    public String getName() {
        return Name;
    }

    public void setName(String Name) {
        this.Name = Name;
    }

    public void setSelectCountry(String selectCountry) {
        this.selectCountry = selectCountry;
    }

    public String getSelectState() {
        return selectState;
    }

    public void setSelectState(String selectState) {
        this.selectState = selectState;
    }

    public Map<String, String> getMapCountry() {
        return mapCountry;
    }

    public void setMapCountry(Map<String, String> mapCountry) {
        this.mapCountry = mapCountry;
    }

    public Map<String, String> getMapState() {
        return mapState;
    }

    public void setMapState(Map<String, String> mapState) {
        this.mapState = mapState;
    }

    /**
     * Creates a new instance of CountryManagedBean
     */
    public CountryManagedBean() {
    }

    /**
     * ********************************************
     */
    public void create() {
        try {
            obj = new Country();
            obj.setCountry(Name);

            this.countryFacade.create(obj);
            set_list();
            Name = "";

            set_message(FacesMessage.SEVERITY_INFO, "Create Religion", "Create " + obj.getCountry()+ " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Create Religion", "Create " + obj.getCountry() + " error !!!");
        }
    }

    public void edit() {
        try {
            this.countryFacade.edit(obj);

            set_message(FacesMessage.SEVERITY_INFO, "Edit Religion", "Edit " + obj.getCountry() + " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Edit Religion", "Edit " + obj.getCountry() + " error !!!");
        }
    }

    public void remove(Country c) {
        try {
            this.countryFacade.remove(c);
            set_list();

            set_message(FacesMessage.SEVERITY_INFO, "Remove Religion", "Remove " + obj.getCountry()+ " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Remove Religion", "Remove " + obj.getCountry() + " error !!!");
        }
    }

    /**
     * ********************************************
     */
    public void setCountryAndState() {
        List<Country> lstCountry = new ArrayList<Country>();
        lstCountry = this.countryFacade.findAll();

        for(Country c : lstCountry){
            mapCountry.put(c.getCountry(), c.getCountry());

            List<State> lstState = new ArrayList<State>();
            lstState = c.getStateList();
            Map<String, String> detailState = new HashMap<String, String>();
            for(State s : lstState){
                detailState.put(s.getState(), s.getState());

                suburbsData.put(c.getCountry(), detailState);
            }
        }
    }
    
    public void handleStateChange() {
        if (selectCountry != null && !selectCountry.equals("")) {
            mapState = suburbsData.get(selectCountry);
        } else {
            mapState = new HashMap<String, String>();
        }
    }
    
    public Country get_NewObjCountry(){
        try {
            return countryFacade.find(new Country(Integer.valueOf(selectCountry)));
        } catch (Exception e) {
            return null;
        }
    }
    
    public State get_NewObjState(){
        try {
            return stateFacade.find(new State(Integer.valueOf(selectState)));
        } catch (Exception e) {
            return null;
        }
    }

    public void set_list() {
        try {
            list = this.countryFacade.findAll();
        } catch (Exception e) {
            list = null;
        }
    }
    
    private void set_message(FacesMessage.Severity severity, String title, String message) {
        try {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, title, message));
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Error message !!!"));
        }
    }
    
}
